package com.cx.service.impl;

import com.cx.mapper.GroupSponsorMapper;
import com.cx.pojo.GroupSponsor;
import com.cx.pojo.GroupSponsorExample;
import com.cx.service.GroupSponsorService;
import com.cx.vo.GroupListVO;
import com.cx.vo.MyGroupInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GroupSponsorServiceImpl implements GroupSponsorService {

    @Autowired
    GroupSponsorMapper groupSponsorMapper;

    @Override
    public int insertGroup(GroupSponsor groupSponsor) {
        int affectedRows = groupSponsorMapper.insertSelective(groupSponsor);
        return affectedRows;
    }

    @Override
    public List<GroupListVO> selectGroupByGroupGoodId(Integer groupGoodId) {
        List<GroupListVO> groupListVOList = groupSponsorMapper.selectGroupByGroupGoodId(groupGoodId);
        return groupListVOList;
    }

    @Override
    public Integer updateJoinNum(Integer id) {
        Integer affectedRows = groupSponsorMapper.updateJoinNum(id);
        return affectedRows;
    }

    @Override
    public Integer checkAndUpdateStatus() {
        GroupSponsor groupSponsor = new GroupSponsor();
        groupSponsor.setGroupSponsorStatus(-1);
        groupSponsor.setUpdateTime(new Date());

        GroupSponsorExample example = new GroupSponsorExample();
        GroupSponsorExample.Criteria criteria = example.createCriteria();
        criteria.andGroupSponsorStatusEqualTo(1);
        criteria.andEndTimeLessThan(new Date());

        Integer affectedRows = groupSponsorMapper.updateByExampleSelective(groupSponsor,example);
        return affectedRows;
    }

    @Override
    public Integer failAndUpdateStatus(Integer groupSponsorId) {
        Integer affectedRows = groupSponsorMapper.failAndUpdateStatus(groupSponsorId);
        return affectedRows;
    }

    @Override
    public Integer successAndUpdateStatus(Integer groupSponsorId) {
        Integer affectedRows = groupSponsorMapper.successAndUpdateStatus(groupSponsorId);
        return affectedRows;
    }

    @Override
    public List<MyGroupInfoVO> selectMyGroupInfo(Integer userId,Integer status) {
        List<MyGroupInfoVO> myGroupInfoVOList = groupSponsorMapper.selectMyGroupInfo(userId,status);
        return myGroupInfoVOList;
    }


}
